package logic

import (
	"context"
	"shenzhou/C/srv/model/mysql"

	"shenzhou/C/srv/internal/svc"
	"shenzhou/C/srv/shenzhou"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *shenzhou.GoodsListRequest) (*shenzhou.GoodsListResponse, error) {
	// todo: add your logic here and delete this line
	g := mysql.Goods{}
	list, err := g.List(int(in.Page), int(in.PageSize))
	if err != nil {
		logx.Error("获取商品列表失败", err)
		return nil, err
	}
	var goodsList []*shenzhou.GoodsItem

	for _, v := range list {
		goodsList = append(goodsList, &shenzhou.GoodsItem{
			GoodsId:    int64(v.ID),
			GoodsTitle: v.GoodsTitle,
			GoodsCover: v.GoodsCover,
			GoodsPrice: float32(v.GoodsPrice),
		})
	}

	return &shenzhou.GoodsListResponse{
		GoodsList: goodsList,
	}, nil
}
